﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MeTalker.DBUtility;
using System.Data.SqlClient;
using AD.Model;

namespace AD.DAL
{
    public class ManagerUserDAL : DALBase
    {
        public static ManagerUserMod SignUser(ManagerUserMod user, out int errorCode)
        {
            errorCode = -1;
            ManagerUserMod tmp = SignUser(user);
            if (tmp == null)
                return null;
            if (tmp.Passwd.Equals(user.Passwd))
            {
                errorCode = 1;
                return tmp;
            }
            else
                errorCode = -2;
            return null;
        }


        static ManagerUserMod SetModel(DataRow row)
        {
            ManagerUserMod user = new ManagerUserMod();
            user.AutoID = int.Parse(row["AutoID"].ToString());
            if (row["IsValid"] != DBNull.Value)
                user.IsValid = bool.Parse(row["IsValid"].ToString());
            if (row["Passwd"] != DBNull.Value)
                user.Passwd = row["Passwd"].ToString();
            if (row["UserName"] != DBNull.Value)
                user.UserName = row["UserName"].ToString();
            if (row["CreateDate"] != DBNull.Value)
                user.CreateDate = DateTime.Parse(row["CreateDate"].ToString());
            return user;
        }

        public static DataTable GetUsers()
        {
            try
            {
                string sql = "Select * from tab_system_manager_info";// and LoginFlag=0
                return SqlHelper.ExecuteDataTable(sql, null);
            }
            catch
            {
                return null;
            }
        }

        public static ManagerUserMod GetUserByIdToMod(int id)
        {
            DataTable dt = GetUserById(id);
            if (dt == null || dt.Rows.Count <= 0)
            {
                return null;
            }
            foreach (DataRow row in dt.Rows)
            {
                return SetModel(row);
            }
            return null;
        }

        public static DataTable GetUserById(int id)
        {
            try
            {
                string sql = "Select * from tab_system_manager_info where AutoID=" + id;
                return SqlHelper.ExecuteDataTable(sql, null);
            }
            catch
            {
                return null;
            }
        }

        public static DataTable GetUser(string userName)
        {
            try
            {
                string sql = "Select * from tab_system_manager_info where UserName='" + userName + "'";// and LoginFlag=0
                return SqlHelper.ExecuteDataTable(sql, null);
            }
            catch
            {
                return null;
            }
        }

        public static int Update(ManagerUserMod user)
        {
            try
            {
                string sql = "update tab_system_manager_info set UserName='{0}', Passwd='{1}', IsValid={2} where AutoID={3}";
                sql = string.Format(sql, user.UserName, user.Passwd, user.IsValid == true ? 1 : 0, user.AutoID);
                return SqlHelper.ExecuteNonQuery(SQL_CONNECT, CommandType.Text, sql, null);
            }
            catch
            {
                return 0;
            }
        }

        public static int Insert(ManagerUserMod user)
        {
            try
            {
                string sql = "insert into tab_system_manager_info(UserName,Passwd,IsValid,CreateDate=getdate()) values('{0}','{1}',1)";
                sql = string.Format(sql, user.UserName, user.Passwd);
                return SqlHelper.ExecuteNonQuery(SQL_CONNECT, CommandType.Text, sql, null);
            }
            catch
            {
                return 0;
            }
        }

        public static int Delete(ManagerUserMod user)
        {
            try
            {
                return Delete(user.AutoID);
            }
            catch
            {
                return 0;
            }
        }

        public static int Delete(int autoid)
        {
            try
            {
                string sql = "delete tab_system_manager_info where AutoID=" + autoid;
                return SqlHelper.ExecuteNonQuery(SQL_CONNECT, CommandType.Text, sql, null);
            }
            catch
            {
                return 0;
            }
        }

        public static List<ManagerUserMod> GetUsersToList()
        {
            DataTable dt = GetUsers();
            if (dt == null || dt.Rows.Count <= 0)
            {
                return null;
            }
            List<ManagerUserMod> users = new List<ManagerUserMod>();
            foreach (DataRow row in dt.Rows)
            {
                users.Add(SetModel(row));
            }
            return users;
        }

        public static ManagerUserMod SignUser(ManagerUserMod user)
        {
            return SignUserToModel(user.UserName, user.Passwd);
        }

        public static ManagerUserMod SignUserToModel(string userName,string passwd)
        {
            DataTable dt = SignUser(userName, passwd);
            if (dt == null || dt.Rows.Count <= 0)
            {
                return null;
            }
            foreach (DataRow row in dt.Rows)
            {
                return SetModel(row);
            }
            return null;
        }

        public static DataTable SignUser(string userName, string password)
        {
            try
            {
                //string sql = "Select * from tab_system_manager_info where Passwd=@Passwd and UserName=@UserName and IsValid=1";// and LoginFlag=0
                string sql = "Select * from tab_system_manager_info where UserName=@UserName";
                SqlParameter paramUserName = new SqlParameter("@UserName", SqlDbType.VarChar, 100, "UserName");
                paramUserName.Value = userName;
                SqlParameter paramPassword = new SqlParameter("@Passwd", SqlDbType.VarChar, 100, "Passwd");
                paramPassword.Value = password;
                return SqlHelper.ExecuteDataTable(sql, new SqlParameter[] { paramUserName, paramPassword });
            }
            catch
            {
                return null;
            }
        }
    }
}